package com.twu.biblioteca;

import com.twu.biblioteca.storage.StorageService;
import com.twu.biblioteca.storage.StorageServiceFactory;
import com.twu.biblioteca.storage.StorageServiceInMemoryImpl;

import java.util.Scanner;

// Dummy Data
// User account : 111-1111
// Password: Password

public class BibliotecaApp {

    public static final int LISTBOOKS = 1;
    public static final int QUIT = 9;
    public static final int CHECKOUTBOOK = 2;
    public static final int CHECKINBOOK = 3;
    public static final int LISTMOVIES = 4;
    public static final int CHECKINMOVIE = 5;
    public static final int CHECKOUTMOVIE = 6;
    static boolean quit = false;
    private static boolean loggedIn = false;

    public static void main(String[] args) {
        BibliotecaApp bibliotecaApp = new BibliotecaApp();
        System.out.println(bibliotecaApp.getWelcomeMessage());
        while(true) {
            logIn();
            while (quit == false && loggedIn == true) {
                System.out.println(bibliotecaApp.getMenuMessage());

                int selection = bibliotecaApp.getUsersSelection();

                System.out.println(bibliotecaApp.usersChoice(selection));
            }
        }
    }

    private static void logIn() {
        StorageService storage = StorageServiceFactory.getInstance().getStorageService(StorageService.IN_MEMORY);

        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter your account number:");
        String accountNumber = scan.nextLine();


        UserAccount user;
        for(int i =0; i<storage.getUserAccountList().size(); i++){
            if(storage.getUserAccountList().get(i).getAccountNumber().equals(accountNumber)){
                System.out.println("Please enter your password:");
                String password = scan.nextLine();
                user = storage.getUserAccountList().get(i);
                if(user.isPasswordTheSame(password)){
                    loggedIn = true;
                } else{
                    System.out.println("That password is incorrect please try again.");
                }
            }else{
                System.out.println("That username is incorrect please try again.");
            }
        }
    }

    public String usersChoice(int selection) {
        StorageService storage = StorageServiceFactory.getInstance().getStorageService(StorageService.IN_MEMORY);
        if(selection == LISTBOOKS){
            System.out.printf("%-40s %-20s %-10s \n", "Title", "Year", "Author");
            System.out.printf("%-40s %-20s %-10s \n", "--------------------------", "-------", "-----------------");
            storage.toPrintFormatted();
            return "";
        }else if(selection == QUIT){
            quit = true;
            loggedIn = false;
            return "Thankyou for using Biblioteca! Come back soon!";
        } else if(selection == CHECKINBOOK) {
            return checkInBook();
        } else if(selection == CHECKOUTBOOK) {
            return checkOutBook();
        }else if(selection == LISTMOVIES) {
            System.out.printf("%-40s %-20s %-20s %-10s\n", "Title", "Year", "Director", "Rating");
            System.out.printf("%-40s %-20s %-20s %-10s\n", "--------------------------", "-------", "-----------------", "---------");
            storage.toPrintMoviesFormatted();
            return "";
        }else if(selection == CHECKOUTMOVIE) {
            return checkOutMovie();
        }else if(selection == CHECKINMOVIE) {
            return checkInMovie();
        }
        return "Select a valid option!";
    }

    private String checkInMovie() {
        StorageService storage = StorageServiceFactory.getInstance().getStorageService(StorageService.IN_MEMORY);
        String movietitle = getTitle();

        for(int i = 0; i<storage.getBooksList().size(); i++){
            if(storage.getMoviesList().get(i).getMovieTitle().equals(movietitle)){
                return storage.getMoviesList().get(i).checkInMovie();
            }
        }
        return "That movie is not avaliable";
    }

    private String checkOutMovie() {
        StorageService storage = StorageServiceFactory.getInstance().getStorageService(StorageService.IN_MEMORY);
        String movietitle = getTitle();

        for(int i = 0; i<storage.getBooksList().size(); i++){
            if(storage.getMoviesList().get(i).getMovieTitle().equals(movietitle)){
                return storage.getMoviesList().get(i).checkOutMovie();
            }
        }
        return "That movie is not avaliable";
    }

    public String checkOutBook() {
        StorageService storage = StorageServiceFactory.getInstance().getStorageService(StorageService.IN_MEMORY);
        String booktitle = getTitle();

       for(int i = 0; i<storage.getBooksList().size(); i++){
            if(storage.getBooksList().get(i).getBookTitle().equals(booktitle)){
                return storage.getBooksList().get(i).checkOutBook();
            }
        }
        return "That book is not avaliable";
    }

    public String getTitle() {
        System.out.println("What is the title?");
        Scanner scanSelection = new Scanner(System.in);
        return scanSelection.nextLine();
    }

    public String checkInBook() {
        StorageService storage = StorageServiceFactory.getInstance().getStorageService(StorageService.IN_MEMORY);
        String booktitle = getTitle();
        for(int i = 0; i<storage.getBooksList().size(); i++){
            if(storage.getBooksList().get(i).getBookTitle().equals(booktitle)){
                return storage.getBooksList().get(i).checkInBook();
            }
        }
        return "That book is not avaliable";
    }

    private int getUsersSelection() {
        Scanner scanSelection = new Scanner(System.in);
        return scanSelection.nextInt();
    }

    public String getWelcomeMessage() {

        return "Welcome to Biblioteca";
    }

    public String getMenuMessage() {
        return "What would you like to do? \n "+ LISTBOOKS +". Browse books \n " + CHECKOUTBOOK +". Checkout a book \n " + CHECKINBOOK +". Check in a book \n \n " + LISTMOVIES+". Browse movies \n " +  + CHECKINMOVIE+". Check in a movie \n " + CHECKOUTMOVIE+". Checkout a movie \n \n "+QUIT +". Log out";
    }
}
